<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onSubmit"
      @cancel="$emit('close')"
    />
  </div>
</template>
<script>
import dayjs from "dayjs";
import { updateUserInfo } from "@/api/user";

export default {
  name: "UpdateBirthday",
  props: {
    value: {
      type: [String, Number]
    }
  },
  data() {
    return {
      minDate: new Date(1950, 0, 1),
      maxDate: new Date(),
      currentDate: this.value ? new Date(this.value) : new Date()
    };
  },
  created() {},
  methods: {
    // 修改资料
    async onSubmit() {
      this.$toast.loading({
        message: "处理中",
        duration: 0
      });
      try {
        let birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
        const { data: res } = await updateUserInfo({
          birthday
        });
        this.$toast.success("更新成功");
        this.$emit("input", birthday);
        this.$emit("close");
      } catch (error) {
        this.$toast.fail("更新失败");
      }
    }
  },
  components: {}
};
</script>
<style scoped lang="less">
.field-wrap {
  margin: 30px;
  /deep/ .van-field__control {
    padding: 14px;
  }
}
</style>
